package com.zc.operator.service.impl;

import java.util.List;
import java.util.Map;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.zc.operator.domain.ChargeStationPrice;
import com.zc.operator.mapper.ChargeStationPriceMapper;
import com.zc.operator.service.IChargeStationPriceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 场站电区间价设置Service业务层处理
 *
 * @author ruoyi
 * @date 2023-05-17
 */
@Service
public class ChargeStationPriceServiceImpl implements IChargeStationPriceService {
    @Autowired
    private ChargeStationPriceMapper chargeStationPriceMapper;

    /**
     * 查询场站电区间价设置
     *
     * @param id 场站电区间价设置主键
     * @return 场站电区间价设置
     */
    @Override
    public ChargeStationPrice selectChargeStationPriceById(Long id) {
        return chargeStationPriceMapper.selectChargeStationPriceById(id);
    }

    /**
     * 查询场站电区间价设置列表
     *
     * @param chargeStationPrice 场站电区间价设置
     * @return 场站电区间价设置
     */
    @Override
    public List<ChargeStationPrice> selectChargeStationPriceList(ChargeStationPrice chargeStationPrice) {
        return chargeStationPriceMapper.selectChargeStationPriceList(chargeStationPrice);
    }

    /**
     * 新增场站电区间价设置
     *
     * @param chargeStationPrice 场站电区间价设置
     * @return 结果
     */
    @Override
    public AjaxResult insertChargeStationPrice(ChargeStationPrice chargeStationPrice) {
        //判断区间时间是否重复
        //boolean isTime = queryTime(chargeStationPrice);
        if (true) {
            chargeStationPrice.setCreateTime(DateUtils.getNowDate());
            chargeStationPrice.setCreateName(SecurityUtils.getUsername());
            boolean isAdd = chargeStationPriceMapper.insertChargeStationPrice(chargeStationPrice);
            if (isAdd) {
                return AjaxResult.success("添加成功");
            }
        } else {
            return AjaxResult.error("时间区间重复");
        }

        return AjaxResult.error("添加失败");
    }

    /**
     * 修改场站电区间价设置
     *
     * @param chargeStationPrice 场站电区间价设置
     * @return 结果
     */
    @Override
    public AjaxResult updateChargeStationPrice(ChargeStationPrice chargeStationPrice) {
        //判断区间时间是否重复
        //boolean isTime = queryTime(chargeStationPrice);
        if (true) {
            chargeStationPrice.setUpdateTime(DateUtils.getNowDate());
            chargeStationPrice.setUpdateName(SecurityUtils.getUsername());
            boolean isUpdate = chargeStationPriceMapper.updateChargeStationPrice(chargeStationPrice);
            if (isUpdate) {
                return AjaxResult.success("修改成功");
            }
        } else {
            return AjaxResult.error("时间区间重复");
        }

        return AjaxResult.error("修改失败");
    }

    /**
     * 判断区间时间是否重复
     *
     * @param cParam 场站电区间价设置
     * @return 结果
     */
    public boolean queryTime(ChargeStationPrice cParam) {
        ChargeStationPrice param = new ChargeStationPrice();
        List<ChargeStationPrice> list = chargeStationPriceMapper.selectChargeStationPriceList(param);
        for (ChargeStationPrice c : list) {
            if(c.getId().equals(cParam.getId())){
                continue;
            }
            //首位时间相同
            if (c.getStartTime().getTime() == cParam.getStartTime().getTime()
                    || c.getEndTime().getTime() == cParam.getEndTime().getTime()) {
                return true;
            }
            //包含
            if (c.getStartTime().getTime() < cParam.getStartTime().getTime()
                    && c.getEndTime().getTime() > cParam.getEndTime().getTime()) {
                return true;
            }
            if (c.getStartTime().getTime() > cParam.getStartTime().getTime()
                    && c.getEndTime().getTime() < cParam.getEndTime().getTime()) {
                return true;
            }
            //交集
            if (c.getStartTime().getTime() < cParam.getStartTime().getTime()
                    && c.getEndTime().getTime() < cParam.getEndTime().getTime()
                    && c.getEndTime().getTime() > cParam.getStartTime().getTime()) {
                return true;
            }
            if (c.getStartTime().getTime() > cParam.getStartTime().getTime()
                    && c.getEndTime().getTime() > cParam.getEndTime().getTime()
                    && cParam.getEndTime().getTime() > c.getStartTime().getTime()) {
                return true;
            }
        }
        return false;
    }

    /**
     * 批量删除场站电区间价设置
     *
     * @param ids 需要删除的场站电区间价设置主键
     * @return 结果
     */
    @Override
    public int deleteChargeStationPriceByIds(Long[] ids) {
        return chargeStationPriceMapper.deleteChargeStationPriceByIds(ids);
    }

    /**
     * 删除场站电区间价设置信息
     *
     * @param id 场站电区间价设置主键
     * @return 结果
     */
    @Override
    public int deleteChargeStationPriceById(Long id) {
        return chargeStationPriceMapper.deleteChargeStationPriceById(id);
    }

    /**
     * 查询该运营商所有站点
     */
    @Override
    public AjaxResult getStationList(Long id) {
        List<Map<String, Object>> stationList = chargeStationPriceMapper.getStationList(id);
        return AjaxResult.success(stationList);
    }

    @Override
    public AjaxResult getStationListByCode(String code) {
        List<Map<String, Object>> stationList = chargeStationPriceMapper.getStationListByCode(code);
        return AjaxResult.success(stationList);
    }

    @Override
    public ChargeStationPrice getChargeStationPrice(ChargeStationPrice chargeStationPrice) {
        return chargeStationPriceMapper.getChargeStationPrice(chargeStationPrice);
    }

    @Override
    public int getValidationRepeat(ChargeStationPrice chargeStationPrice) {
        List<ChargeStationPrice> validationRepeat = chargeStationPriceMapper.getValidationRepeat(chargeStationPrice);
        return validationRepeat.size();
    }
}
